package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.dal.exception.NetApiException;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.DialogFastBuildUtil.Builder;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.TaskCenterController;
import com.qire.manhua.databinding.ActivityTaskCenterBinding;
import com.qire.manhua.model.viewModel.impl.TaskCenterViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.TaskCenterPage)
public class TaskCenterActivity extends BaseActivity<ActivityTaskCenterBinding> {

    @Autowired(injector = ViewModelInjector.class)
    TaskCenterViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    TaskCenterController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_task_center;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel.onAddition(controller);
        controller.onAddition(viewModel);

        viewModel.loadSignInData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.loadPageData();
    }

    @DalExceptionObserve(tags = Url.v5_receiveAward)
    public void receiveAwardExceptionHandle(DataAccessException e) {
        viewModel.dismissLoadDialog();
        ToastHelper.showSnackBar(String.format("领取失败:%s", e.extraMsg), Prompt.ERROR);
    }

    @DalExceptionObserve(tags = Url.WeChatBind)
    public void weChatBindExceptionHandle(DataAccessException e) {
        if(e instanceof NetApiException) {
            final Builder builder = DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_r);
            builder.setTextView(R.id.dialog_title, "该微信号已注册，无法完成绑定")
                    .setTextVisible(R.id.dialog_content, true)
                    .setTextView(R.id.dialog_button_dark, "取消", view -> {
                        builder.dismiss();
                        view.setOnClickListener(null);
                    })
                    .setTextView(R.id.dialog_button_light, "查看解决方案", view -> {
                        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                                .withData("title", "解决方案")
                                .withData("url", Url.thirdPlatformCanNotBindSolutionURL)
                                .navigation();
                        builder.dismiss();
                        view.setOnClickListener(null);
                    }).show();
        }
        ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
    }

}
